<?php

class funcoes {

    public static function converteParaDataSql($data) {
        if (!ereg("([0-9]{1,2})/([0-9]{1,2})/([0-9]{4})", $data))
            return null;
        $newDate = explode("/", $data);
        if (count($newDate) != 3)
            return null;
        return $newDate[2] . "-" . $newDate[1] . "-" . $newDate[0];
    }

    public static function converteParaDataBR($data) {
        $newDate = self::getArrData($data);
        return $newDate[2] . "/" . $newDate[1] . "/" . $newDate[0];
    }

    public static function getArrData($data) {
        if (!ereg("([0-9]{4})-([0-9]{1,2})-([0-9]{1,2})", $data))
            return null;
        $newDate = explode("-", $data);
        if (count($newDate) != 3)
            return null;
        return $newDate;
    }

    public static function arrayUtf8Decode($array) {
        $newArray = array();
        if (is_array($array) && sizeof($array) > 0)
            foreach ($array as $indx => $campo) {
                $newArray[$indx] = utf8_decode($campo);
            }
        return $newArray;
    }

    public static function arrayUtf8Encode($array) {
        $newArray = array();
        if (is_array($array) && sizeof($array) > 0)
            foreach ($array as $indx => $campo) {
                $newArray[$indx] = utf8_encode($campo);
            }
        return $newArray;
    }

    public static function debuga() {
        $arrDados = func_get_args();
        print("<pre>");
        for ($i = 0; $i < count($arrDados); $i++) {
            print("<BR>");
            print_r($arrDados[$i]);
        }
        die();
    }

    private function getExtensao($arquivo_name) {
        return $extensao = strtolower(end(explode('.', $arquivo_name)));
    }

    public static function salvarImagem($arquivo, $localimg) {
        if (isset($arquivo)) {
            $aux = explode('.', $arquivo["name"]);
            $extensao = strtolower($aux[1]);
            $arquivo_nome = time() . "." . $extensao;
            $pasta_dir = $localimg; //destino             
            //se nao existir a pasta ele cria uma
            if (!file_exists($pasta_dir)) {
                mkdir($pasta_dir);
            }
            $destino = $pasta_dir . $arquivo_nome;
            // Faz o upload da imagem
            move_uploaded_file($arquivo["tmp_name"], $destino);
        } else {
            $arquivo_nome = "";
        }
        return $arquivo_nome;
    }

    public static function deletaImagem($caminho) {
        if (file_exists($caminho)) {
            unlink($caminho);
        }
    }

    public static function gerarTabelaSimples($array, $paranslinks = null, $dir = null) {

        $string = "<table class='table table-bordered'>";
        foreach ($array as $i => $row) {
            $string.= "<tr>";
            //funcoes::debuga($row);
            if (isset($row['img'])) {
                $row['img'] = "<img style='width: 35px;height: 35px;' src='" . $dir . "imagens/" . $row['img'] . "'>";
            }
            $row = array_values($row);
            foreach ($row as $j => $col) {
                $string.="<td> <span style='color:#6d7275;'>" . $col . "</span> </td>";
            }
            foreach ($paranslinks as $l => $link) {
                if (!empty($link['icone'])) {
                    if ($link['txt'] === 'excluir') {
                        $string.="<td width='30'> <a  href='" . URL . $link['link'] . $row[0] . "' onclick='return confirm(" . '"' . "Deseja realmente excluir esse registro?" . '"' . ")'><img src='" . $dir . "icones/" . $link['icone'] . "'></a> </td>";
                    } else {
                        $string.="<td width='30'> <a  href='" . URL . $link['link'] . $row[0] . "'><img src='" . $dir . "icones/" . $link['icone'] . "'></a> </td>";
                    }
                } else {
                    $string.="<td> <a class='btn btn-info' href='" . URL . $link['link'] . $row[0] . "'>" . $link['txt'] . "</a> </td>";
                }
            }
            $string.="</tr>";
        }
        $string.="</table>";

        return $string;
    }

    public static function gerarTabelaSimples1($array, $paranslinks = null, $dir = null) {
        $string = "";
        foreach ($array as $i => $row) {
            $string.= "<tr>";
            //funcoes::debuga($row);
            if (isset($row['img'])) {
                $row['img'] = "<img style='width: 35px;height: 35px;' src='" . $dir . "imagens/" . $row['img'] . "'>";
            }
            $row = array_values($row);
            foreach ($row as $j => $col) {
                $string.="<td class='text-center'>" . $col . " </td>";
            }
            $string.= "<td width='80'> ";
            $string.= "<a href='" . URL . $paranslinks[0]['link'] . $row[0] . "'><img src='" . $dir . "icones/" . $paranslinks[0]['icone'] . "'></a>";
            $string.= "<a href='" . URL . $paranslinks[1]['link'] . $row[0] . "' onclick='return confirm(" . '"' . "Deseja realmente excluir esse registro?" . '"' . ")'><img src='" . $dir . "icones/" . $paranslinks[1]['icone'] . "'></a> ";
            $string.= "<a href='" . URL . $paranslinks[2]['link'] . $row[0] . "'><img src='" . $dir . "icones/" . $paranslinks[2]['icone'] . "'></a>";
            $string.= "</td>";
            $string.="</tr>";
        }
        return $string;
    }

    public static function anti_injection($sql) {
        $sql = preg_replace(sql_regcase("/(from|select|insert|delete|where|drop table|show tables|#|\*|--|\\\\)/"), "", $sql);
        $sql = trim($sql);
        $sql = strip_tags($sql);
        $sql = addslashes($sql);
        return $sql;
    }

    public static function anti_sql_injection($str) {
        if (!is_numeric($str)) {
            $str = get_magic_quotes_gpc() ? stripslashes($str) : $str;
            $str = function_exists('mysql_real_escape_string') ? mysql_real_escape_string($str) : mysql_escape_string($str);
        }
        return $str;
    }

}
